﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *  Copyright (C) 2010 by Barbara Post postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;
using OsamesFrameworkCore.Logging;
using OsamesFrameworkCore.Messaging;
using OsamesFrameworkCore.Xml.Types;

namespace OsamesFrameworkCore.Plugins
{
    /// <summary>
    /// Définit des fonctions communes aux plugins pour éviter de les implémenter dans chaque plugin.
    /// </summary>
    public abstract class AbstractPlugin : IPlugin
    {
        private readonly List<Message> _lstMessages = new List<Message>();

        #region déclaration propriétés et méthodes abstraites
        
        /// <summary>
        /// Initialize does nothing by default. Overridable.
        /// </summary>
        public virtual void Initialize()
        {
        }
        public abstract void Dispose();

        /// <summary>
        /// Plugin algorithm call.
        /// </summary>
        /// <param name="strMethodName_">Name of function declared in the plugin to execute by reflection</param>
        /// <param name="oParameters_">Function arguments</param>
        public virtual object Execute(string strMethodName_, object[] oParameters_)
        {
            // TODO
            return null;
        }
        
        #endregion

        #region propriétés/fonctions communes aux plugins
        /// <summary>
        /// Plugin host : PluginsManager singleton.
        /// </summary>
        public IPluginHost Host { get; set; }
        /// <summary>
        /// Configuration for plugin, stored in a dictionary.
        /// </summary>
        public XmlDictionaryContainer XmlDictPluginConfiguration { get; set; }
        /// <summary>
        /// List of messages produced by plugin.
        /// </summary>
        public List<Message> MessagesList
        {
            get { return _lstMessages; }
        }

        #endregion

        /// <summary>
        /// Ajoute un message dans la liste des messages (_lstMessages)
        /// </summary>
        /// <param name="eMessageType_"></param>
        /// <param name="strMessageText_"></param>
        /// <param name="eMessageDisplaylocation_"></param>
        public void AddMessage(MessageType eMessageType_, string strMessageText_, MessageDisplayLocation eMessageDisplaylocation_)
        {
            _lstMessages.Add(new Message(eMessageType_, strMessageText_, eMessageDisplaylocation_));
        }
        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="eLogLevel_">Osames log level</param>
        /// <param name="strMessage_">Message</param>
        /// <param name="strMessageDetails_">Optional array of message details. In this case, strMessage_ parameter contains placeholders, each matching a value in strMessageDetails_</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, object[] strMessageDetails_ = null)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, strMessageDetails_);
        }

        /// <summary>
        /// Logging facility.
        /// </summary>
        /// <param name="eLogLevel_">Osames log level</param>
        /// <param name="strMessage_">Message</param>
        /// <param name="ex_">Exception we will log message and stack trace of</param>
        public void Log(LogLevel eLogLevel_, string strMessage_, Exception ex_)
        {
            LoggerService.Log(this, eLogLevel_, strMessage_, ex_);
        }

    }
}